/*
	Twitch
	Please leave credits to the origional devs where its due if edited.
*/

// Make sure we are using mysql, if not exit this file.
if (!GM.UseSQL or GM.SQLType != 'MySQL') then
	return
end

require("tmysql")

if !tmysql then
	Error("Twitch MySQL Error - tMySQL module not found while MySQL was enabled!")
	return
end

GM.MySQL = {}
GM.MySQL.Connections	= 6
GM.MySQL.Username		= "root"
GM.MySQL.Password		= "password"
GM.MySQL.Database		= "Twitch"
GM.MySQL.Threads		= 5
GM.MySQL.Host			= "127.0.0.1"
GM.MySQL.Port			= 3306

tmysql.initialize(GM.MySQL.Host,GM.MySQL.Username,GM.MySQL.Password,GM.MySQL.DatabaseGM.MySQL.Port,GM.MySQL.Connections,GM.MySQL.Threads)

function GM.MySQL.LoadPlayer(ply)
	if !ply or !ply:SteamID() then return end
	
	local SteamID = tmysql.escape(ply:SteamID())
	local Name = tmysql.escape(ply:Nick())
	
	tmysql.query("SELECT * FROM twitch WHERE SteamID = \'"..tostring(SteamID).."\'",function(res,status,error)
		if res[1] then // User found, set their info.

			ply:SetMoney(res[1][3])

		else // No user found, create their info.

			local Money = 100

			tmysql.query("INSERT INTO twitch(SteamID, Name, Money ) values('"..tostring(SteamID).."', '"..tostring(Name).."', "..tostring(Money).."' )", QueryErrors(res,status,error))
			
			ply:SetMoney(Money)

			chat.AddText(ply,Color(50,255,50,0),"Welcome to Twitch, your account has been created and saved!")

		end
	end)
end

function GM.MySQL.SavePlayer(ply)

	local Name = tmysql.escape(self:Nick())
	local SteamID = tmysql.escape(self:SteamID())
	local Money = self:GetMoney()

	tmysql.query("UPDATE twitch set Name = \'"..tostring(Name).."\', Money = "..tostring(Money).."  where SteamID = \'"..tostring(SteamID).."\'")
	chat.AddText(self,Color(50,255,50,0),"Saved Profile!")

end
